//
//  CYSettingTableVC.m
//  CreditAddressBook
//
//  Created by Lee on 15/7/5.
//  Copyright (c) 2015年 Lee. All rights reserved.
//

#import "CYSettingTableVC.h"
#import "CYJumpsTool.h"
#import "CYLoginHttpTool.h"
#import "CYModifyPasswordVC.h"
#import "UMFeedback.h"
#import "TOWebViewController.h"

@interface CYSettingTableVC ()<UIAlertViewDelegate>

@property (weak, nonatomic) IBOutlet UIButton *exitBtn;
@property (weak, nonatomic) IBOutlet UILabel *versionLabel;

@property (strong, nonatomic) CYUser *user;
@end

@implementation CYSettingTableVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupLoginButton];

//    NSString *appVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleVersionKey];//获取build
    NSString *appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];//获取version
    self.versionLabel.text = appVersion;
}

- (void)setupLoginButton {
    self.exitBtn.layer.masksToBounds = YES;
    self.exitBtn.layer.cornerRadius = 4.f;
    CYUser *user = [CYLoginHttpTool userData].user;
    self.user = user;
    if (user) {
        [self.exitBtn setTitle:@"退出" forState:UIControlStateNormal];
        [self.exitBtn addTarget:self action:@selector(exitClick:) forControlEvents:UIControlEventTouchUpInside];
    } else {
        [self.exitBtn setTitle:@"登录" forState:UIControlStateNormal];
        [self.exitBtn addTarget:self action:@selector(loginClick:) forControlEvents:UIControlEventTouchUpInside];
    }
}

#pragma mark - Table view data source

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section != 0 && !self.user) {
        if (indexPath.section == 1 || indexPath.row == 1 ) {
            [CYJumpsTool jumpToLogin:self];
        } else {
            
        }
    } else if(indexPath.section == 1) {
         CYUser *user = [CYLoginHttpTool userData].user;
         if (user) {
              CYModifyPasswordVC *modify = [[CYModifyPasswordVC alloc] init];
              [self.navigationController pushViewController:modify animated:YES];
         }else{
              [CYJumpsTool jumpToLogin:self];
              
         }
       
    }
//    else if (indexPath.section == 2 && indexPath.row == 1) {
//        [self.navigationController pushViewController:[UMFeedback feedbackViewController] animated:YES];
//    }
    else if (indexPath.section == 2 && indexPath.row == 0) {
        TOWebViewController *webViewController = [[TOWebViewController alloc] initWithURLString:@"http://www.credit-manage.com/app/help/help.html"];
        webViewController.showUrlWhileLoading = NO;
        webViewController.navigationButtonsHidden = YES;
        webViewController.title = @"加载中...";
        [self.navigationController pushViewController:webViewController animated:YES];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 2) {
        return 20;
    }
    return .1f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (void)exitClick:(UIButton *)sender {
    UIAlertView *logoutAlert = [[UIAlertView alloc]initWithTitle:@"确认退出当前账号？" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
//    logoutAlert.tag = CYLogoutAlertTag;
    [logoutAlert show];
}

- (void)loginClick:(UIButton *)sender {
    [CYJumpsTool jumpToLogin:self];
}

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        //登出后清除本地归档信息
        [CYLoginHttpTool isClearUserData];
        //登出后清除本地归档信息
        NSUserDefaults *deft = [NSUserDefaults standardUserDefaults];
        [deft removeObjectForKey:CYUserNameKey];
        [deft removeObjectForKey:CYPasswordKey];
        [deft synchronize];
        
        //跳转到Main
        [CYJumpsTool jumpToMain:self.view];
    }
}

@end
